Page({
    data: {
        currentMonth: '',
        selectedDateIndex: 0,
        weekList: [],
        roomList: []
    },

    onLoad() {
        this.initWeekList();
        this.fetchRooms();
    },

    onShow(){
        this.onLoad();
    },

    initWeekList() {
        const weekNames = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
        const today = new Date();
        const weekList = [];
        for (let i = 0; i < 6; i++) {
            const date = new Date();
            date.setDate(today.getDate() + i);
            const day = date.getDate();
            const week = weekNames[date.getDay()];
            weekList.push({
                day,
                week
            });
            // console.log(weekList[0].day)
        }

        this.setData({
            weekList,
            currentMonth: `${today.getFullYear()}.${String(today.getMonth() + 1).padStart(2, '0')}`
        });
    },

    onDateSelect(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            selectedDateIndex: index
        });
        console.log(this.data.currentMonth)
        console.log(this.data.weekList[index].day);
        console.log(this.data.weekList[index].week);
        // 可添加按日期刷新房间的逻辑
        this.fetchRooms();
    },

    fetchRooms() {
        wx.request({
            url: 'http://localhost:8080/branch/list', // 替换为你的真实接口
            method: 'GET',
            data: {
                day: this.data.weekList[this.data.selectedDateIndex].day,
            },
            success: res => {
                //   this.setData({ roomList: res.data.data });
                const formattedRooms = res.data.data.map(item => ({
                    ...item, // 保留其他字段
                    startTime: item.startTime.slice(0, 5),
                    endTime: item.endTime.slice(0, 5)
                }));
                this.setData({
                    roomList: formattedRooms
                });
            },
            fail: () => {
                wx.showToast({
                    title: '获取房间失败',
                    icon: 'none'
                });
            }
        });
    },

    onReservation(e) {
        console.log(this.data.weekList[this.data.selectedDateIndex].day)
        const userId = wx.getStorageSync('userId');
        const branchId = e.currentTarget.dataset.branchid;
        const day = this.data.weekList[this.data.selectedDateIndex].day;

        console.log(day)

        wx.request({
            url: 'http://localhost:8080/reservation/add',
            method: 'POST',
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: {
                userId,
                branchId,
                day
            },
            success: (res) => {
                if (res.data.code === 0) { 
                    this.fetchRooms();
                    wx.showToast({
                        title: '预约成功',
                        icon: 'success'
                    });
                } else {
                    wx.showToast({
                        title: res.data.msg || '操作失败',
                        icon: 'none'
                    });
                }
            },
            fail: err => {
                wx.showToast({
                    title: '网络异常',
                    icon: 'fail',
                });
            }
        });
    },

    onMyRes(){
        wx.navigateTo({
          url: '/pages/home/myReservation/myReservation',
        })
    }

});